class CreateScoreSubmitters < ActiveRecord::Migration
  def self.up
     submitters_up()
     create_submitters()
     demo_link_submitters()
  end

  def self.submitters_up()
    create_table :submitters do |t|
      t.string :name
      t.text :description
      t.string :password
      t.boolean :is_administrator

      t.timestamps
    end

    create_table :events_submitters do |t|
#      t.integer :submitter_id
#      t.integer :event_id
      t.references :submitter
      t.references :event
                                      # Accepted and Rejected are what admins do when reviewing submissions
      t.datetime :accepted            # not accepted/rejected if == nil
      t.datetime :rejected            # if rejected, event_id -> nil (otherwise, kept for statistics)

      t.timestamps
    end
  end

  def self.down
    drop_table :events_submitters
    drop_table :submitters
  end

  def self.create_submitters()
    [
      # :name                        :description                     :is_admin   :password [ == :name.lower.stripspaces().SHA1_hexdigest ]
      [ "Mister Piston",             "Derby Tracker",                 true,       "a58b44346770c9281d710cae037dc139ae3a0109" ],
      [ "Hurt Reynolds",             "Derby Tracker Extraordinaire",  true,       "6849733f0da042f7a073112a5544f9490ae7774b" ],
      [ "Bulldog",                   "",                              false,      "ddf45997a7e18a25ad5f5cf222da64814dd060d5" ],
      [ "Justin",                    "",                              false,      "0ce7911e6479995d6c346d6f03eb723b5135309e" ],
    ].each do | n, d, a, p |
      aSubmitter = Submitter.create(:name => n, :description => d, :is_administrator => a, :password => p)
      ## wierd.. this corrupts p -- need to explicitly set!
      aSubmitter.password = p
      aSubmitter.save;
      # save not needed with create (only with new)
    end
  end

  def self.demo_link_submitters()
    [
      # :name                       :event                  :date - must be m/d/y, 2nd optional if same day 
      [ "Mister Piston",            "1",                    "2/1/08",   "" ],                 # event by ID
      [ "Hurt Reynolds",            "Rust Riot",            "2/1/08",   "" ],                 # ... by name
      [ "Bulldog",                  "",                     "",         "" ],                 # ... by name
      [ "Justin",                   "",                     "",         "" ],                 # ... by ID
      [ "Mister Piston",            "2",                    "1/17/08",  "1/17/08" ],          # ... by ID
    ].each do | n, e, d1, d2 |
      subList = Submitter.find(:all, :conditions => ["name LIKE ?", '%' + n + '%'])
      raise "Need better Submitter name for #{n} (no matches)" if subList.empty?
      raise "Need better Submitter name for #{n} (matches too many - #{subList.join(', ') })" if subList.length > 1

      if (!e.empty?)
        evList = Event.find(:all, :conditions => ["id = ?", e])
        if (!evList || evList.empty?)
          evList = Event.find(:all, :conditions => ["name LIKE ?", '%' + e + '%'])
          raise "Need better event string for #{n} (#{e} matches too many - #{evList.join(', ')})" if evList.length > 1
        end

        if (evList && !evList.empty? && evList[0])
          raise "Need a start date for <'#{n}', '#{e}'>" if d1 == ""

          event = evList[0]
          begin
          date1 = DateTime.strptime(d1, "%m/%d/%y")
          event.start_date = date1
          if (d2 == "")
            event.end_date = date1
          else
            event.end_date = DateTime.strptime(d2, "%m/%d/%y")
          end
          rescue
            $stderr.puts "Date failed: #{d1}"
          end
          event.save()

          sub = subList[0]
          sub.events << event
          sub.save()
        end
      end
    end
  end
end

# vim:ts=2:et:
